#pragma once


#include<assert.h>
#include<iostream>

using namespace std;

namespace yyh
{
	template<class T>
	struct list_node
	{
		list_node(const T& val = T())
			:_prev(nullptr)
			, _pnext(nullptr)
			, _data(val)
		{ }

		list_node<T>* _prev;
		list_node<T>* _pnext;
		T _data;
	};

	template<class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, Ptr> Self;

		list_iterator(Node* node)
			:_node(node)
		{ }

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		Self& operator++()
		{
			_node = _node->_pnext;
			return *this;
		}

		Self operator++(int)
		{
			Self temp(*this);
			_node = _node->_pnext;
			return temp;
		}

		Self operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			Self temp(*this);
			_node = _node->_prev;
			return temp;
		}

		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}

		bool operator==(const Self& it)
		{
			return _node != it._node;
		}

		Node* _node;
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;

		iterator begin()
		{
			iterator it(_head->_pnext);
			return it;
		}

		iterator end()
		{
			iterator it(_head);
			return it;
		}

		const_iterator begin()const
		{
			const_iterator it(_head->_pnext);
			return it;
		}

		const_iterator end()const
		{
			const_iterator it(_head);
			return it;
		}

		void empty_init()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_pnext = _head;
			_size = 0;
		}

		list()
		{
			empty_init();
		}

		list(const list<T>& lt)
		{
			empty_init();

			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		list(initializer_list<T> lt)
		{
			empty_init();

			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		~list()
		{
			clear();

			delete _head;
			_head = nullptr;
			_size = 0;
		}

		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		list<T>& operator=(list<T>& lt)
		{
			swap(lt);
			return *this;
		}

		size_t size()
		{
			return _size;
		}

		void insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* rev = pos._node->_prev;

			Node* new_node = new Node(x);

			rev->_pnext = new_node;
			new_node->_pnext = cur;
			new_node->_prev = rev;
			cur->_prev = new_node;

			_size++;
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());

			Node* cur = pos._node;
			Node* rev = cur->_prev;
			Node* next = cur->_pnext;

			rev->_pnext = next;
			next->_prev = rev;

			delete cur;

			_size--;

			return iterator(next);
		}

	private:
		Node* _head;
		size_t _size;
	};

	void test()
	{
		list<int> l1;

		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		for (auto& e : l1)
		{
			cout << e << ' ';
		}
		cout << endl;

		list<int> l2(l1);
		for (auto& e : l2)
		{
			cout << e << ' ';
		}
		cout << endl;

		list<int> l3 = { 1,2,3,4 };
		for (auto& e : l3)
		{
			cout << e << ' ';
		}
		cout << endl;

		list<int> l4;
		l4 = l3;
		for (auto& e : l4)
		{
			cout << e << ' ';
		}
		cout << endl;

		auto it = l1.begin();
		it++;
		l1.erase(it);
		for (auto& e : l1)
		{
			cout << e << ' ';
		}
		cout << endl;

		l1.pop_back();
		for (auto& e : l1)
		{
			cout << e << ' ';
		}
		cout << endl;

		l1.pop_front();
		for (auto& e : l1)
		{
			cout << e << ' ';
		}
		cout << endl;

		l1.push_front(10);
		for (auto& e : l1)
		{
			cout << e << ' ';
		}
		cout << endl;
	}
}